package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_mass_mailing_contact;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_mass_mailing_contactSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_mass_mailing_contactService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[群发邮件联系人] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_mass_mailing_contactServiceImpl implements IMail_mass_mailing_contactService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_mass_mailing_contactClientService mail_mass_mailing_contactClientService;

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_mass_mailing_contact clientModel = mail_mass_mailing_contactClientService.createModel();
        clientModel.setId(id);
        mail_mass_mailing_contactClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Mail_mass_mailing_contact get(Integer id) {
        Imail_mass_mailing_contact clientModel = mail_mass_mailing_contactClientService.createModel();
        clientModel.setId(id);
        mail_mass_mailing_contactClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_mass_mailing_contact get(Mail_mass_mailing_contact mail_mass_mailing_contact) {
        Mail_mass_mailing_contact retmail_mass_mailing_contact = this.get(mail_mass_mailing_contact.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_mass_mailing_contact.getClass(), mail_mass_mailing_contact.getClass(), false);
        copier.copy(retmail_mass_mailing_contact, mail_mass_mailing_contact, null);
        return mail_mass_mailing_contact ;
    }

    @Override
    public boolean update(Mail_mass_mailing_contact mail_mass_mailing_contact) {
        boolean bOk = false ;
        Imail_mass_mailing_contact clientModel = this.convert2Model(mail_mass_mailing_contact,null);
        mail_mass_mailing_contactClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_mass_mailing_contact) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_mass_mailing_contactClientService.removeBatch(mail_mass_mailing_contacts) ;

    }

    @Override
    public void createBatch(List<Mail_mass_mailing_contact> mail_mass_mailing_contacts, int batchSize) {

                        //mail_mass_mailing_contactClientService.createBatch(mail_mass_mailing_contacts,batchSize) ;

    }

    @Override
    public void updateBatch(List<Mail_mass_mailing_contact> mail_mass_mailing_contacts, int batchSize) {

        //mail_mass_mailing_contactClientService.updateBatch(mail_mass_mailing_contacts,batchSize) ;

    }

    @Override
    public boolean create(Mail_mass_mailing_contact mail_mass_mailing_contact) {
        boolean bOk = false ;
        Imail_mass_mailing_contact clientModel = this.convert2Model(mail_mass_mailing_contact,null);
        mail_mass_mailing_contactClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_mass_mailing_contact);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_mass_mailing_contact> searchDefault(Mail_mass_mailing_contactSearchContext context) {
        List<Mail_mass_mailing_contact> list = new ArrayList<Mail_mass_mailing_contact>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact> clientModelList = mail_mass_mailing_contactClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_contact>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据国家/地区查询
     */
    @Override
    public Page<Mail_mass_mailing_contact> selectByOdoo_country(Integer id){
        Mail_mass_mailing_contactSearchContext context = new Mail_mass_mailing_contactSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_contact> list = new ArrayList<Mail_mass_mailing_contact>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact> clientModelList = mail_mass_mailing_contactClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_contact>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据业务伙伴称谓查询
     */
    @Override
    public Page<Mail_mass_mailing_contact> selectByOdoo_title(Integer id){
        Mail_mass_mailing_contactSearchContext context = new Mail_mass_mailing_contactSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_contact> list = new ArrayList<Mail_mass_mailing_contact>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact> clientModelList = mail_mass_mailing_contactClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_contact>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_mass_mailing_contact> selectByOdoo_create(Integer id){
        Mail_mass_mailing_contactSearchContext context = new Mail_mass_mailing_contactSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_contact> list = new ArrayList<Mail_mass_mailing_contact>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact> clientModelList = mail_mass_mailing_contactClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_contact>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mail_mass_mailing_contact> selectByOdoo_write(Integer id){
        Mail_mass_mailing_contactSearchContext context = new Mail_mass_mailing_contactSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_mass_mailing_contact> list = new ArrayList<Mail_mass_mailing_contact>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact> clientModelList = mail_mass_mailing_contactClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_mass_mailing_contact clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_mass_mailing_contact>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_mass_mailing_contact convert2Model(Mail_mass_mailing_contact domain , Imail_mass_mailing_contact model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_mass_mailing_contactClientService.createModel() ;
        }

         if(domain.getIs_blacklistedDirtyFlag())
            model.setIs_blacklisted(domain.getIs_blacklisted());
         if(domain.getMessage_has_errorDirtyFlag())
            model.setMessage_has_error(domain.getMessage_has_error());
         if(domain.getMessage_channel_idsDirtyFlag())
            model.setMessage_channel_ids(domain.getMessage_channel_ids());
         if(domain.getMessage_partner_idsDirtyFlag())
            model.setMessage_partner_ids(domain.getMessage_partner_ids());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getMessage_main_attachment_idDirtyFlag())
            model.setMessage_main_attachment_id(domain.getMessage_main_attachment_id());
         if(domain.getMessage_bounceDirtyFlag())
            model.setMessage_bounce(domain.getMessage_bounce());
         if(domain.getMessage_needaction_counterDirtyFlag())
            model.setMessage_needaction_counter(domain.getMessage_needaction_counter());
         if(domain.getMessage_follower_idsDirtyFlag())
            model.setMessage_follower_ids(domain.getMessage_follower_ids());
         if(domain.getMessage_unread_counterDirtyFlag())
            model.setMessage_unread_counter(domain.getMessage_unread_counter());
         if(domain.getTag_idsDirtyFlag())
            model.setTag_ids(domain.getTag_ids());
         if(domain.getList_idsDirtyFlag())
            model.setList_ids(domain.getList_ids());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIs_email_validDirtyFlag())
            model.setIs_email_valid(domain.getIs_email_valid());
         if(domain.getMessage_idsDirtyFlag())
            model.setMessage_ids(domain.getMessage_ids());
         if(domain.getMessage_is_followerDirtyFlag())
            model.setMessage_is_follower(domain.getMessage_is_follower());
         if(domain.getOpt_outDirtyFlag())
            model.setOpt_out(domain.getOpt_out());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getSubscription_list_idsDirtyFlag())
            model.setSubscription_list_ids(domain.getSubscription_list_ids());
         if(domain.getWebsite_message_idsDirtyFlag())
            model.setWebsite_message_ids(domain.getWebsite_message_ids());
         if(domain.getEmailDirtyFlag())
            model.setEmail(domain.getEmail());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getMessage_needactionDirtyFlag())
            model.setMessage_needaction(domain.getMessage_needaction());
         if(domain.getMessage_has_error_counterDirtyFlag())
            model.setMessage_has_error_counter(domain.getMessage_has_error_counter());
         if(domain.getCompany_nameDirtyFlag())
            model.setCompany_name(domain.getCompany_name());
         if(domain.getMessage_attachment_countDirtyFlag())
            model.setMessage_attachment_count(domain.getMessage_attachment_count());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getMessage_unreadDirtyFlag())
            model.setMessage_unread(domain.getMessage_unread());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getTitle_id_textDirtyFlag())
            model.setTitle_id_text(domain.getTitle_id_text());
         if(domain.getCountry_id_textDirtyFlag())
            model.setCountry_id_text(domain.getCountry_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getTitle_idDirtyFlag())
            model.setTitle_id(domain.getTitle_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCountry_idDirtyFlag())
            model.setCountry_id(domain.getCountry_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Mail_mass_mailing_contact convert2Domain( Imail_mass_mailing_contact model ,Mail_mass_mailing_contact domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_mass_mailing_contact();
        }

        if(model.getIs_blacklistedDirtyFlag())
            domain.setIs_blacklisted(model.getIs_blacklisted());
        if(model.getMessage_has_errorDirtyFlag())
            domain.setMessage_has_error(model.getMessage_has_error());
        if(model.getMessage_channel_idsDirtyFlag())
            domain.setMessage_channel_ids(model.getMessage_channel_ids());
        if(model.getMessage_partner_idsDirtyFlag())
            domain.setMessage_partner_ids(model.getMessage_partner_ids());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getMessage_main_attachment_idDirtyFlag())
            domain.setMessage_main_attachment_id(model.getMessage_main_attachment_id());
        if(model.getMessage_bounceDirtyFlag())
            domain.setMessage_bounce(model.getMessage_bounce());
        if(model.getMessage_needaction_counterDirtyFlag())
            domain.setMessage_needaction_counter(model.getMessage_needaction_counter());
        if(model.getMessage_follower_idsDirtyFlag())
            domain.setMessage_follower_ids(model.getMessage_follower_ids());
        if(model.getMessage_unread_counterDirtyFlag())
            domain.setMessage_unread_counter(model.getMessage_unread_counter());
        if(model.getTag_idsDirtyFlag())
            domain.setTag_ids(model.getTag_ids());
        if(model.getList_idsDirtyFlag())
            domain.setList_ids(model.getList_ids());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIs_email_validDirtyFlag())
            domain.setIs_email_valid(model.getIs_email_valid());
        if(model.getMessage_idsDirtyFlag())
            domain.setMessage_ids(model.getMessage_ids());
        if(model.getMessage_is_followerDirtyFlag())
            domain.setMessage_is_follower(model.getMessage_is_follower());
        if(model.getOpt_outDirtyFlag())
            domain.setOpt_out(model.getOpt_out());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getSubscription_list_idsDirtyFlag())
            domain.setSubscription_list_ids(model.getSubscription_list_ids());
        if(model.getWebsite_message_idsDirtyFlag())
            domain.setWebsite_message_ids(model.getWebsite_message_ids());
        if(model.getEmailDirtyFlag())
            domain.setEmail(model.getEmail());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getMessage_needactionDirtyFlag())
            domain.setMessage_needaction(model.getMessage_needaction());
        if(model.getMessage_has_error_counterDirtyFlag())
            domain.setMessage_has_error_counter(model.getMessage_has_error_counter());
        if(model.getCompany_nameDirtyFlag())
            domain.setCompany_name(model.getCompany_name());
        if(model.getMessage_attachment_countDirtyFlag())
            domain.setMessage_attachment_count(model.getMessage_attachment_count());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getMessage_unreadDirtyFlag())
            domain.setMessage_unread(model.getMessage_unread());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getTitle_id_textDirtyFlag())
            domain.setTitle_id_text(model.getTitle_id_text());
        if(model.getCountry_id_textDirtyFlag())
            domain.setCountry_id_text(model.getCountry_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getTitle_idDirtyFlag())
            domain.setTitle_id(model.getTitle_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCountry_idDirtyFlag())
            domain.setCountry_id(model.getCountry_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
